package cn.mesmile.flink.transform;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author zb
 * @date 2021/8/22 15:23
 * @Description flatMap 适用于 一对多 转换
 */
public class FlinkTransformRichFlatMapApp {

    /**
     * Rich相关的api更丰富，多了Open、Close方法，用于初始化连接等
     * RichXXX相关Open、Close、setRuntimeContext等 API方法会根据并行度进行操作的
     *   比如并行度是4，那就有4次触发对应的open/close方法等，是4个不同subtask
     *   比如 RichMapFunction、RichFlatMapFunction、RichSourceFunction等
     */

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        env.setParallelism(1);

        DataStreamSource<String> stringDataStreamSource = env.fromElements("12,34,56", "test,one", "789,000");

        // flatMap 适用于 一对多 转换
        DataStream<Tuple2<String, Integer>> mapDs = stringDataStreamSource.flatMap(new RichFlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> collector) throws Exception {
                String[] split = value.split(",");
                for (String key : split) {
                    collector.collect(new Tuple2<>(key, 1));
                }
            }
            // alt + insert
            @Override
            public void open(Configuration parameters) throws Exception {
                super.open(parameters);
                System.out.println("------------open-------------");
            }

            @Override
            public void close() throws Exception {
                super.close();
                System.out.println("------------close-------------");
            }
        });


        mapDs.print("result:");

        env.execute("custom map job");
    }
}
